import time
import uuid
from iot import settings
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.parsers import MultiPartParser
from TOOL.main import implant_image
from TOOL.analysis import analysis_func
from TOOL.OSS import put_object_oss
from django.core.files.base import ContentFile
from django.core.files.storage import default_storage



class implant(APIView):

    def post(self, request):
        # 从请求中获取图片文件对象
        image_file = request.FILES.get('image')
        bit_stream = request.data.get('bit_stream')
        image_file_handle, bit = implant_image(image_file.read(), bit_stream)
        file_name = f"{uuid.uuid4()}_{image_file}"
        # 构造图片访问链接并返回
        image_url = put_object_oss(ObjectName=file_name, LocalFile=image_file_handle)
        return Response({'image_url': image_url, 'bit': eval(bit)})


class analysis(APIView):
    def post(self, request):
        # 从请求中获取图片文件对象
        image_file = request.FILES.get('image')
        bit = analysis_func(image_file.read())
        return Response({'bit': bit})
